package com.lottery.im.module.chat.ui;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.Api;
import com.lottery.im.api.result.entity.ReceivedRedPacketsReturnMap;
import com.lottery.im.api.result.entity.SingleRedPacketsReceived;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.chat.adapter.RedPacketsReceivedAdapter;
import com.lottery.im.module.chat.presenter.RedPacketsDetailPresenter;
import com.lottery.im.module.chat.view.RedPacketsDetailView;
import com.lottery.im.util.transformations.CropCircleTransformation;
import com.tencent.imsdk.TIMConversationType;
import java.text.DecimalFormat;

/**
 * 红包领取详情
 */
public class RedPacketsDetailFragment extends BaseSwipeFragment<RedPacketsDetailPresenter>
    implements RedPacketsDetailView {

  private final static String TYPE_KEY = "TYPE_KEY";
  private final static String REDPACKETS_KEY = "REDPACKETS_KEY";
  private final static String NOTE_KEY = "NOTE_KEY";

  public static RedPacketsDetailFragment create(TIMConversationType type,
      SingleRedPacketsReceived redPacketsReceived,String note) {
    RedPacketsDetailFragment fragment = new RedPacketsDetailFragment();
    Bundle b = new Bundle();
    b.putSerializable(TYPE_KEY, type);
    b.putSerializable(REDPACKETS_KEY, redPacketsReceived);
    b.putString(NOTE_KEY,note);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_money) TextView mTvMoney;
  @BindView(R.id.tv_name) TextView mTvName;
  @BindView(R.id.tv_num) TextView mTvNum;
  @BindView(R.id.tv_note) TextView mTvNote;
  @BindView(R.id.list) RecyclerView mList;
  @BindView(R.id.iv_portrait) ImageView mIvPortrait;

  private TIMConversationType mType;
  private SingleRedPacketsReceived mRedPacketsReceived;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_redpackets_detail;
  }

  @Override protected void initPresenter() {
    presenter = new RedPacketsDetailPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    Bundle arguments = getArguments();
    if (null == arguments) {
      popBackStack();
    } else {
      mType = (TIMConversationType) arguments.getSerializable(TYPE_KEY);
      mRedPacketsReceived = (SingleRedPacketsReceived) arguments.getSerializable(REDPACKETS_KEY);
      mList.setLayoutManager(new LinearLayoutManager(mActivity));
      RecyclerViewDivider.with(mActivity)
          .color(ContextCompat.getColor(mActivity, R.color.windowBackground))
          .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
          .hideLastDivider()
          .build()
          .addTo(mList);
      Glide.with(this)
          .load(Api.BASEADDRESS + mRedPacketsReceived.getUserImg())
          .apply(new RequestOptions()
              .bitmapTransform(new CropCircleTransformation())
              .placeholder(R.drawable.icon_conversation_de)
              .error(R.drawable.icon_conversation_de))
          .into(mIvPortrait);
      mTvName.setText(mRedPacketsReceived.getUserName() + "的红包");

      String note = arguments.getString(NOTE_KEY);
      if (TextUtils.isEmpty(note)){
        mTvNote.setText("恭喜发财，大吉大利");
      }else {
        mTvNote.setText(note);
      }

      if (mType == TIMConversationType.C2C) {
        mTvNum.setText("");
        presenter.querySingleRedPacketsDetail(mRedPacketsReceived.getRedPacketsId());
      } else {
        presenter.queryRedPacketsDetail(mRedPacketsReceived.getRedPacketsId());
      }
    }
  }

  @OnClick({ R.id.iv_back, R.id.tv_record }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_record:
        startFragment(new RedPacketsRecordFragment());
        break;
    }
  }

  @Override public void queryRedPacketsDetailSuce(
      ReceivedRedPacketsReturnMap receivedRedPacketsReturnMap) {
    if (mType == TIMConversationType.Group) {
      mTvNum.setText("共" + receivedRedPacketsReturnMap.getTotalNumber() + "个红包，剩余"
          + receivedRedPacketsReturnMap.getRemainNumber() + "个");
    }

    DecimalFormat df = new DecimalFormat("#0.00");
    mTvMoney.setText(df.format(receivedRedPacketsReturnMap.getTotalMoney()));
    mList.setAdapter(
        new RedPacketsReceivedAdapter(receivedRedPacketsReturnMap.getReceivedRedPacketsList()));
  }
}
